package com.bjsxt.shop.common.util;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 最渴望的是CRUD
 * 
 * @author WangshSxt
 *
 */
@Component("redisUtil")
public class RedisUtil
{
	@Resource
	private RedisTemplate redisTemplate ;
	
	/**
	 * 往Redis中放入一个值(键值对)
	 * @param key
	 * @param value
	 */
	public boolean set(final String key,final String value)
	{
		return (boolean) this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueBytes = redisTemplate.getStringSerializer().serialize(value);
					connection.set(keyBytes, valueBytes);
					return true ; 
				} catch (Exception e)
				{
					e.printStackTrace();
				}
				return false;
			}
		});
	}
	
	/**
	 * 设置键值对,并且指定过期时间
	 * 
	 * @param key
	 * @param value
	 * @param timeout	指定多长时间过期,单位是秒
	 */
	public boolean set(final String key,final String value, final int timeout)
	{
		return (boolean) this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueBytes = redisTemplate.getStringSerializer().serialize(value);
					connection.set(keyBytes, valueBytes);
					/* 指定一下过期时间 */
					return connection.expire(keyBytes, timeout);
				} catch (Exception e)
				{
				}
				return false;
			}
		});
	}
	
	/**
	 * 设置键值对,并且指定过期时间
	 * 
	 * @param key
	 * @param value
	 * @param timeout	指定哪个时间点过期,单位是秒
	 */
	public boolean set(final String key,final String value, final Date timeout)
	{
		return (boolean) this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueBytes = redisTemplate.getStringSerializer().serialize(value);
					connection.set(keyBytes, valueBytes);
					/* 指定一下过期时间 */
					return connection.expireAt(keyBytes, timeout.getTime());
				} catch (Exception e)
				{
				}
				return false;
			}
		});
	}
	
	/**
	 * 通过key取值
	 * @param key
	 * @return	字符串
	 */
	public String get(final String key)
	{
		return "" + this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueByte = connection.get(keyBytes);
					/* 将字符数组转换成字符串 */
					String value = redisTemplate.getStringSerializer().deserialize(valueByte) + "";
					return value ; 
				} catch (Exception e)
				{
				}
				return "";
			}
		});
	}
	
	/**
	 * 通过key取值
	 * @param key
	 * @return	字符串
	 */
	public String delete(final String key)
	{
		return "" + this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					return connection.del(keyBytes) ; 
				} catch (Exception e)
				{
				}
				return "";
			}
		});
	}
	
}
